Option Strict Off
Option Explicit On


Imports System.Runtime.InteropServices


Friend Class frmAnimator
    Inherits System.Windows.Forms.Form 'Declares that all variables must be defined before use.

    'UPGRADE_ISSUE: Declaring a parameter 'As Any' is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="FAE78A8D-8978-4FD4-8208-5B7324A8F795"'
    Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Integer, ByVal X As Integer, ByVal Y As Integer, ByRef lpPoint As Object) As Integer
    Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Integer, ByVal X As Integer, ByVal Y As Integer) As Integer







    <MarshalAs(UnmanagedType.FunctionPtr)> Private callback As KeyboardHookDelegate



    Private Structure POINT
        Dim X As Integer
        Dim Y As Integer
    End Structure


    Dim TimeLapseString As String 'Holds the string for the caption of the live window
    Dim OnionSkinningString As String 'Holds the string for the caption of the live window
    Dim FrameSliderPos As Object 'The position of the frame slider image
    Dim OnionSliderPos As Object 'The position of the onion slider image

    Dim FrameSliderLeft As Integer
    Dim FrameSliderTop As Integer
    Dim FrameSliderWidth As Integer
    Dim FrameSliderHeight As Integer
    Dim FrameSliderPosition As Integer
    Dim LastXSheetPos As Integer 'Stores the last X sheet position, so it is not redrawn if it doesn't need to do. This avoids flickering
    Dim CaptionStore As String 'Stores the Live/Playback title caption for comparison
    Dim ToolText As String 'Tool Tip Text




    Private Sub frmAnimator_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load 'Sets up the form with the buttons. This is an irregular shape so
        'the form must be frameless ,background pic must be borderless,
        'AutoDraw must be True, and ScaleMode must be vbPixels

        On Error GoTo ErrorHandling 'If an error occurs go to the error handling routine

        ErrorCodeNo = "04.00" 'Set an error code for the next section of program

        Dim sngPlaybackDisplayTime As String 'Holds the playback position used to display the time in playback title bar
        Dim sngFrameDisplay As String 'Holds the frame number string to display in the playback title bar


        FrameSliderLeft = 72
        FrameSliderTop = 48
        FrameSliderWidth = 279
        FrameSliderHeight = 15




        'UPGRADE_WARNING: Add a delegate for AddressOf KeyboardProc Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="E9E157F7-EF0C-4016-87B7-7D7FBBC6EE08"'

        callback = New KeyboardHookDelegate(AddressOf KeyboardProc)


        hHook = SetWindowsHookEx(2, callback, VB6.GetHInstance.ToInt32, System.Threading.Thread.CurrentThread.ManagedThreadId) '2 = keyboard
        mblnRunning = False 'Ensure the main loop is not running during setup

        'UPGRADE_ISSUE: Form property frmAnimator.ScaleMode is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="8027179A-CB3B-45C0-9863-FAA1AF983B59"'
        'frmAnimator.ScaleMode = 3 'Set form mode of the form with the buttons in it to pixels
        Me.Width = VB6.TwipsToPixelsX(500 * VB6.TwipsPerPixelX) 'Set the buttons form width (Convert width to twips as screen works in twips)
        Me.Height = VB6.TwipsToPixelsY(165 * VB6.TwipsPerPixelY) 'Set the buttons form height (Convert height to twips as screen works in twips)

        Me.Left = VB6.TwipsToPixelsX((VB6.PixelsToTwipsX(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width) * (FormAnimatorLeft / 100)) - (VB6.PixelsToTwipsX(Me.Width) / 2)) 'The FormAnimatorLeft variable is loaded from file and is a percentage of the screen width
        Me.Top = VB6.TwipsToPixelsY((VB6.PixelsToTwipsY(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height) * (FormAnimatorTop / 100)) - (VB6.PixelsToTwipsY(Me.Height) / 2)) 'to the centre of the form, so calculate the actual top left position of the form



        'UPGRADE_ISSUE: Constant vbPixels was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="55B59875-9A95-4B71-9D6A-7C294BF7139D"'
        'UPGRADE_ISSUE: PictureBox property picFacia.ScaleMode was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'        
        'UPGRADE_ISSUE: PictureBox property picFacia.AutoSize was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        picFacia.AutoSize = True : picFacia.Left = 0 : picFacia.Top = 0
        'UPGRADE_ISSUE: PictureBox property picFacia.AutoRedraw was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'picFacia.AutoRedraw = True

        picFacia.BorderStyle = System.Windows.Forms.BorderStyle.None
        picFacia.Visible = True
        picFacia.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None
        'UPGRADE_WARNING: Picture has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
        picFacia.BackgroundImage = System.Drawing.Image.FromFile(My.Application.Info.DirectoryPath & "\Skins\" & Skin & "_Buttons_S.jpg") 'Setup the correct picture size etc.

        'UPGRADE_WARNING: Control property picFacia.Parent was upgraded to picFacia.FindForm which has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="DFCDE711-9694-47D7-9C50-45A99CD8E91E"'
        SetWindowRgn(picFacia.FindForm.Handle.ToInt32, RegionButtons, True) 'Set the region of the button box outside which nothing will be displayed. The region has been
        'pre calculated during the initial load. See fmrMainForm



        AspFix = False 'Disable the aspect ratio module so that the windows can be initially sized correctly


        WindowAtFront = "Live" 'Set the status variable to tell the program the live window is on top.
        AspFix = True


        frmCapture.Text = "Live Window - " & CaptureWidth & " X " & CaptureHeight & " - Onion Skinning Off" 'Set caption of Capture window

        'Enable the aspratio subroutine module to fix the aspect ratios of the windows if dragged by the user


        Me.Show() 'The form is now set up so display the form to the user.
        Me.Activate() 'Set the focus to the buttons window (Really to just makes sure playback and capture tiles are grey!)


        ShowingLive = False 'Used to control the playback looping


        'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
        If Dir(ChromaBackground) = "" Then 'if the chroma background file doesn't exist switch chroma keying off
            ChromaBackground = "No Background Image Selected"
            ChromaType = "Off" 'switch chroma keying off
        End If




        'UPGRADE_ISSUE: PictureBox property picSlider.AutoRedraw was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'frmAveraging.picSlider.AutoRedraw = True
        frmAveraging.picSlider.Image = System.Drawing.Image.FromFile(My.Application.Info.DirectoryPath & "\Skins\" & Skin & "_Slider.bmp")

        Dim frameSlideGraph As Graphics = Me.picFrameSlider.CreateGraphics
        Dim frameSlideHDC As IntPtr = frameSlideGraph.GetHdc
        Dim avgGraph As Graphics = frmAveraging.picSlider.CreateGraphics
        Dim avgHDC As IntPtr = avgGraph.GetHdc
        Dim onionGraph As Graphics = Me.picOnionSlider.CreateGraphics
        Dim onionHDC As IntPtr = avgGraph.GetHdc

        'Print the initial frame slider control
        'UPGRADE_ISSUE: Constant vbWhiteness was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="55B59875-9A95-4B71-9D6A-7C294BF7139D"'
        'UPGRADE_ISSUE: PictureBox property picFrameSlider.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        Call BitBlt(frameSlideHDC, 0, 0, Me.picFrameSlider.Width, Me.picFrameSlider.Height, 0, 0, 0, vbWhiteness) 'overwite the slider to erase the previous position
        'UPGRADE_ISSUE: Constant vbSrcCopy was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="55B59875-9A95-4B71-9D6A-7C294BF7139D"'
        'UPGRADE_ISSUE: PictureBox property picSlider.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'UPGRADE_ISSUE: PictureBox property picFrameSlider.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        Call BitBlt(frameSlideHDC, Me.picFrameSlider.Width - 6, 1, 6, 13, avgHDC, 0, 0, vbSrcCopy) 'blit the slider to the screen
        Me.picFrameSlider.Refresh()

        
        

        'Print the initial onion slider control
        'UPGRADE_ISSUE: Constant vbWhiteness was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="55B59875-9A95-4B71-9D6A-7C294BF7139D"'
        'UPGRADE_ISSUE: PictureBox property picOnionSlider.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        Call BitBlt(onionhdc, 0, 0, Me.picOnionSlider.Width, Me.picOnionSlider.Height, 0, 0, 0, vbWhiteness) 'overwite the slider to erase the previous position
        'UPGRADE_ISSUE: Constant vbSrcCopy was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="55B59875-9A95-4B71-9D6A-7C294BF7139D"'
        'UPGRADE_ISSUE: PictureBox property picSlider.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'UPGRADE_ISSUE: PictureBox property picOnionSlider.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        Call BitBlt(onionhdc, ((OnionLevel / 255) * Me.picOnionSlider.Width) - 3, 1, 6, 13, avgHDC, 0, 0, vbSrcCopy) 'blit the slider to the screen
        Me.picOnionSlider.Refresh()

        frameSlideGraph.ReleaseHdc(frameSlideHDC)
        frameSlideGraph.Dispose()
        avgGraph.ReleaseHdc(avgHDC)
        avgGraph.Dispose()
        onionGraph.ReleaseHdc(onionHDC)
        onionGraph.Dispose()

        Me.picFacia.Refresh()


        mblnRunning = True 'Start the main program loop

        '*************************************main loop start**************************************************************
        Do While mblnRunning 'This a continually running loop to enable the alphablend (onionskinning)

InEditor:

            Call frmDiagnostics.ShowDiagnostics() 'Show variables in the Diagnostics window

            ErrorCodeNo = "04.01" 'Set an error code for the next section of program
            If ExitHotkeyPressed = True Then
                ErrorCodeNo = "00.00" 'Tell the program it is a normal exit
                Call ExitProgram() 'The exit hotkey has been pressed, so exit the program
            End If
            System.Windows.Forms.Application.DoEvents() 'Check for other events, Mouse, Keyboard etc. dont include more than one in loop as it makes the program sluggish

            ErrorCodeNo = "04.02" 'Set an error code for the next section of program
            Call ButtonWindowDragging() 'Routine to enable the user to drag the button window around the screen

            ErrorCodeNo = "04.03" 'Set an error code for the next section of program
            Call ActionExposureSheet() 'Update the exposure sheet so it scrolls and shows the correct mouthshape

            ErrorCodeNo = "04.04" 'Set an error code for the next section of program
            Call frmEditor.ActionEditor() 'Action any looping requirements the editor may need

            If ActiveWindowIs = "EditorWindow" Then GoTo InEditor 'Stop most actions whilst in editor (makes editor much faster at playback etc.)

            Call UpdateLCDScreen() 'update the LCD display on the buttons form. This also handles "Tool tips"


            ErrorCodeNo = "04.05" 'Set an error code for the next section of program
            Call ActionFrameSlider() 'Check if the user has pressed the frame slider



            ErrorCodeNo = "04.06" 'Set an error code for the next section of program
            Call PositionOnionSlider() 'Check and move the onion slider on the buttons form

            ErrorCodeNo = "04.07" 'Set an error code for the next section of program
            Call PlayBackModeLooping() 'Action the looping function in playback mode

            ErrorCodeNo = "04.08" 'Set an error code for the next section of program
            If GetAsyncKeyState(VK_LBUTTON) Then GoTo DontBringToFront 'If the left button is being pressed, dont bring button window to front as user may be
            'activating a button on another form and doing so wil lose form focus so form button wont respond.
            If ActiveWindowIs = "ButtonWindow" And RotoScopeActiveYesNo = False Then
                'if the buttons window is active, ie settings,utilities or editor are not open and also the rotoscope window is not open then bring buttons window to the foreground
                'Check for rotoscope avoids sluggish response of buttons on rotoscope form
                BringWindowToTop(Me.Handle.ToInt32) 'Bring the buttons window to the front (also focuses it)
                '  frmAnimator.ZOrder 0               '<<<<<<<<<<<<<<<<<<<<this also works!
            End If
DontBringToFront:



            '****************routines for getting image to the immediate "capture" window*************************************


            ErrorCodeNo = "04.09" 'Set an error code for the next section of program
            If WindowAtFront = "Live" Then Call GetImageFromCamera() 'Dont stream from camera if the live window is not showing (This is slow as it waits for camera ie 5 fps)

            ErrorCodeNo = "04.11" 'Set an error code for the next section of program
            Call frmCapture.frmCaptureResize() 'Check and adjust window if the user is resizing the live /playback window

            ErrorCodeNo = "04.12" 'Set an error code for the next section of program
            Call ActionRotoscoping()

            ErrorCodeNo = "04.13" 'Set an error code for the next section of program
            Call ChecksWhenPlayingAndLooping() 'Perform checks if the animation is playing or looping (moving slider etc.)


            'Add  markers to picBuffer before sending to the screen
            '    SetPixel frmAveraging.picBuffer.hDC, 50, 50, RGB(255, 0, 0)

            ErrorCodeNo = "04.14" 'Set an error code for the next section of program
            Call PrintMainWindowCaption() 'Print the main window title bar caption

            'ErrorCodeNo = "04.15"                                                             'Set an error code for the next section of program
            '  Call frmDiagnostics.ShowDiagnostics                                                            'Show variables in the Diagnostics window
            ErrorCodeNo = "04.99" 'Set an error code for the next section of program
        Loop
        '*************************************main loop end**************************************************************

        On Error GoTo 0 'Set error handling so it doesnt return here.
        Exit Sub

        '***********************************************************************************************************************************************

ErrorHandling:
        ErrorDescription = "Main Program Loop Problem"
        Call frmErrorBox.ErrorMessageBox() 'Put up a message Box
        Call ExitProgram() 'Exit the Program gracefully

    End Sub

    Private Sub GetImageFromCamera() 'Pulls image from camera to the live window. Note this is slow as it can hold the program for the camera
        'to respond. This can be at 5 frames per second. So dont do this you dont want to, ie only in live mode.
        ErrorCodeNo = "04.09a"
        If ActiveWindowIs <> "ButtonWindow" Then Exit Sub 'Chroma keying is only active in button form, not editor or settings etc.
        If WindowAtFront <> "Live" Then Exit Sub 'Chroma Keying is only active in live mode

        Dim mainGraph As Graphics = frmAveraging.picBufferMain.CreateGraphics
        Dim mainhdc As IntPtr = mainGraph.GetHdc

        If OnionMode <> 1 Then
            'UPGRADE_ISSUE: PictureBox property picBufferMain.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
            CopyCamToPic((frmAveraging.picBufferMain)) 'Copy the camera image to the picBufferMain picturebox (no onionskinning)
            ErrorCodeNo = "04.09b"
            Call ActionChromaKeying((frmAveraging.picBufferMain)) 'Action any chroma keying

        Else
            'UPGRADE_ISSUE: PictureBox property picBufferMain.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
            CopyCamToPic((frmAveraging.picBufferMain))
            ErrorCodeNo = "04.09b"
            On Error GoTo 0
            Call ActionChromaKeying((frmAveraging.picBufferMain)) 'Action any chroma keying

            AlphaFlags.BlendOp = &H0 : AlphaFlags.BlendFlags = 0 : AlphaFlags.AlphaFormat = 0
            AlphaFlags.SourceConstantAlpha = OnionLevel 'original value was 128 reducing makes source overlay more transparent

            
            Dim onionGraph As Graphics = frmAveraging.picOnion1.CreateGraphics
            Dim onionhdc As IntPtr = onionGraph.GetHdc

            'UPGRADE_WARNING: Couldn't resolve default property of object AlphaFlags. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
            Call RtlMoveMemory(AlphaFlagsAsLong, AlphaFlags, 4) 'copy the BLENDFUNCTION-structure to a Long as Alpha blend only accepts a long value not a flag array
            'UPGRADE_ISSUE: PictureBox property picOnion1.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
            'UPGRADE_ISSUE: PictureBox property picBufferMain.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
            Call AlphaBlend(mainhdc, 0, 0, CaptureWidth, CaptureHeight, onionhdc, 0, 0, CaptureWidth, CaptureHeight, AlphaFlagsAsLong)

            onionGraph.ReleaseHdc(onionhdc)
            onionGraph.Dispose()
        End If

        Dim liveGraph As Graphics = frmCapture.picLive.CreateGraphics
        Dim livehdc As IntPtr = liveGraph.GetHdc

        ErrorCodeNo = "04.10" 'Set an error code for the next section of program
        'copy the image from the picBufferMain picturebox to the piclive visible window, stretched to the correct size
        Call DrawSafeRectangle((frmAveraging.picBufferMain), SafeAreaWidth, SafeAreaHeight, SafeAreaColour)
        'UPGRADE_ISSUE: Constant STRETCHMODE was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="55B59875-9A95-4B71-9D6A-7C294BF7139D"'
        'UPGRADE_ISSUE: PictureBox property picLive.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        Call SetStretchBltMode(livehdc, Declares.StretchMode.STRETCH_HALFTONE)
        'UPGRADE_ISSUE: Constant vbSrcCopy was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="55B59875-9A95-4B71-9D6A-7C294BF7139D"'
        'UPGRADE_ISSUE: PictureBox property picBufferMain.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'UPGRADE_ISSUE: PictureBox property picLive.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        Call StretchBlt(livehdc, 0, 0, frmCapture.picLive.Width, frmCapture.picLive.Height, mainhdc, 0, 0, CaptureWidth, CaptureHeight, vbSrcCopy)

        liveGraph.ReleaseHdc(livehdc)
        liveGraph.Dispose()
        mainGraph.ReleaseHdc(mainhdc)
        mainGraph.Dispose()

        frmCapture.picLive.Refresh()

    End Sub
    Private Sub PrintMainWindowCaption() 'Prints the main window title bar caption
        Dim sngPlaybackDisplayTime As String 'Holds the playback position used to display the time in playback title bar
        Dim sngFrameDisplay As String 'Holds the frame number string to display in the playback title bar
        Dim NewCaption As String

        If WindowAtFront = "Live" Then
            If TimeLapseOnYesNo = True Then 'Check if timelapse mode is on
                TimeLapseString = " - Time Lapse Mode On - Time Interval " & Int(TimeLapseDelay / 1000) & " seconds"
            Else
                TimeLapseString = ""
            End If

            If OnionMode = -1 Then 'Check if onion skinning is on
                OnionSkinningString = " - Onion Skinning Off"
            Else
                OnionSkinningString = " - Onion Skinning On at " & Int(OnionLevel / 2.55) & "%"
            End If

            NewCaption = "Live Window - " & CaptureWidth & " X " & CaptureHeight & OnionSkinningString & TimeLapseString
            If NewCaption <> CaptionStore Then 'Only print it if the text has changed to avoid flickering
                frmCapture.Text = NewCaption 'Print the caption
                CaptionStore = NewCaption 'Store it for the next comparison
            End If
        Else
            'The playback window is at the front
            If WindowAtFront = "PlayBack" Then
                sngPlaybackDisplayTime = VB6.Format(Str(PlayBackMediaPosition.CurrentPosition), "##########################0.000")


                If PlayBackMediaPosition.CurrentPosition = PlayBackMediaPosition.Duration Then
                    sngFrameDisplay = LTrim(Str(Fix((PlayBackMediaPosition.CurrentPosition * FPS) + 0.00001)))
                Else
                    sngFrameDisplay = LTrim(Str(1 + (Fix((PlayBackMediaPosition.CurrentPosition * FPS) + 0.00001))))
                End If
                If Len(sngFrameDisplay) = 1 Then sngFrameDisplay = "00000" & sngFrameDisplay
                If Len(sngFrameDisplay) = 2 Then sngFrameDisplay = "0000" & sngFrameDisplay
                If Len(sngFrameDisplay) = 3 Then sngFrameDisplay = "000" & sngFrameDisplay
                If Len(sngFrameDisplay) = 4 Then sngFrameDisplay = "00" & sngFrameDisplay
                If Len(sngFrameDisplay) = 5 Then sngFrameDisplay = "0" & sngFrameDisplay


                NewCaption = "Playback Window - " & "Frame " & sngFrameDisplay & "             " & sngPlaybackDisplayTime & " Seconds" '      - " & "Frame " & Int(PlayBackMediaPosition.CurrentPosition * FPS) + 1
            Else
                NewCaption = "Playback Window - Awaiting Video"
            End If
            If NewCaption <> CaptionStore Then 'Only print it if the text has changed to avoid flickering
                frmCapture.Text = NewCaption 'Print the caption
                CaptionStore = NewCaption 'Store it for the next comparison
            End If
        End If


    End Sub
    Private Sub ChecksWhenPlayingAndLooping() 'Whilst in playmode perform actions
        If FrameNum < 1 Then Exit Sub 'If there are no frames captured take no action
        'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
        If PlayBackMediaControl Is Nothing Then Exit Sub 'The media player is not loaded so exit sub

        If (PlayStatus = "Playing") Or (PlayStatus = "Looping") Then Call AudioPlayer_Sync() 'If we are looping or playing the animation, syncronise the audio

        If (PlayStatus = "Playing") And (PlayBackMediaPosition.CurrentPosition <> PlayBackMediaPosition.Duration) Then 'Check if a playing animation is not at the end
            Call PositionFrameSlider(1) 'Reposition the slider
            Exit Sub
        End If

        If (PlayStatus = "Playing") And (PlayBackMediaPosition.CurrentPosition = PlayBackMediaPosition.Duration) Then 'Check if a playing animation has reached the end
            PlayStatus = "Stopped"
            Call AudioPlayer_Stop() 'Stop the soundtrack
            Call PositionFrameSlider(1) 'Reposition the slider
            Exit Sub
        End If

        If (PlayStatus = "Looping") And (PlayBackMediaPosition.CurrentPosition <> PlayBackMediaPosition.Duration) Then 'Check if a looping animation is not at the end
            Call PositionFrameSlider(1) 'Reposition the slider
            Exit Sub
        End If


    End Sub


    Private Sub picFacia_MouseUp(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.MouseEventArgs) Handles picFacia.MouseUp
        Dim Button As Short = eventArgs.Button \ &H100000
        Dim Shift As Short = System.Windows.Forms.Control.ModifierKeys \ &H10000
        Dim X As Single = eventArgs.X
        Dim Y As Single = eventArgs.Y 'Mouse button has been released over the buttons window picturebox skin

        If TimeLapseOnYesNo = True Then Exit Sub 'If in timelapse mode all buttons are disabled
        If IsCapturing = True Then Exit Sub 'The program is still capturing / saving, so dont action buttons

        If ActiveWindowIs <> "ButtonWindow" Then Exit Sub 'If the buttons window is not active, dont action any buttons

        If Button = VB6.MouseButtonConstants.LeftButton Then 'The mouse button is the left button

            Red = 0 : Green = 0 : Blue = 0
            Call GetCursorPos(MousePoint) 'Get the screen position of the cursor

            Call ScreenToClient(Me.picFacia.Handle.ToInt32, MousePoint) 'Modify values so they are relative to top left of Buttons picture window
            PixCol = GetPixel(Hotspot_Buttons, MousePoint.X, MousePoint.Y) 'Get the relative colour at the mouse point of the hotspot image in memory
            Red = PixCol And &HFF : Green = CShort(PixCol And &HFF00) / 256 : Blue = CShort(PixCol And &HFF0000) / 65535 'Get the value of each color byte (rgb)

            'Buttons window buttons
            If Red = 255 And Green = 0 And Blue = 0 Then Call TextWind() 'You are over the text window and have released the mouse button
            If Red = 0 And Green = 255 And Blue = 0 Then Call Toggle() 'Toggle Button has been released
            If Red = 0 And Green = 0 And Blue = 255 Then Capture = False 'Capture Button has been released
            If Red = 255 And Green = 255 And Blue = 0 Then
                ErrorCodeNo = "00.00" 'Tell the program it is a normal exit
                Call ExitProgram() 'Exit Button has been released
            End If
            If Red = 0 And Green = 255 And Blue = 255 Then Call OnionButt() 'Onion Button has been released
            If Red = 255 And Green = 0 And Blue = 255 Then Call Settings() 'Settings Button has been released
            If Red = 128 And Green = 0 And Blue = 128 Then Call Utilities() 'Utilities Button has been released
            If Red = 50 And Green = 0 And Blue = 50 Then Call Editor() 'Editor Button has been released
            If Red = 0 And Green = 100 And Blue = 0 Then Call ToStart() 'To Start Button has been released
            If Red = 100 And Green = 0 And Blue = 0 Then Call Back1() 'Back 1 Button has been released
            If Red = 0 And Green = 50 And Blue = 0 Then Call StopButt() 'Stop Button has been released
            If Red = 50 And Green = 0 And Blue = 0 Then Call PlayButt() 'Play Button has been released
            If Red = 0 And Green = 0 And Blue = 50 Then Call Forward1() 'Forward 1 Button has been released
            If Red = 0 And Green = 0 And Blue = 100 Then Call ToEnd() 'To end Button has been released
            If Red = 100 And Green = 100 And Blue = 0 Then Call LoopButt() 'Loop Button has been released
            If Red = 200 And Green = 0 And Blue = 255 Then Call RotoScope() 'Rotoscope Button has been released
            If Red = 46 And Green = 8 And Blue = 144 Then Call Undo() 'Undo Button has been released
            If Red = 246 And Green = 147 And Blue = 134 Then Call SoundToggle() 'Sound Toggle Button has been released
            If Red = 79 And Green = 14 And Blue = 11 Then Call XSheetToggle() 'X Sheet Toggle Button has been released
            If Red = 241 And Green = 250 And Blue = 0 Then Call ChromaKey() 'Chroma Key button has been released
        End If
    End Sub
    Private Sub UpdateLCDScreen() 'tooltips

        If ActiveWindowIs <> "ButtonWindow" Then Exit Sub 'If the buttons window is not active, dont action any buttons


        Red = 0 : Green = 0 : Blue = 0
        Call GetCursorPos(MousePoint) 'Get the screen position of the cursor

        Call ScreenToClient(Me.picFacia.Handle.ToInt32, MousePoint) 'Modify values so they are relative to top left of Buttons picture window
        PixCol = GetPixel(Hotspot_Buttons, MousePoint.X, MousePoint.Y) 'Get the relative colour at the mouse point of the hotspot image in memory
        Red = PixCol And &HFF : Green = CShort(PixCol And &HFF00) / 256 : Blue = CShort(PixCol And &HFF0000) / 65535 'Get the value of each color byte (rgb)

        'Buttons window buttons
        ToolText = ""
        If Red = 0 And Green = 255 And Blue = 0 Then ToolText = "Toggle Live/Playback Mode" 'Toggle Button has been released
        If Red = 0 And Green = 0 And Blue = 255 Then ToolText = "Capture a Frame" 'Capture Button has been released
        If Red = 255 And Green = 255 And Blue = 0 Then ToolText = "Exit the Program"
        If Red = 0 And Green = 255 And Blue = 255 Then ToolText = "Onion Skinning On/Off" 'Onion Button has been released
        If Red = 255 And Green = 0 And Blue = 255 Then ToolText = "Open Settings Window" 'Settings Button has been released
        If Red = 128 And Green = 0 And Blue = 128 Then ToolText = "Open Utilities Window" 'Utilities Button has been released
        If Red = 50 And Green = 0 And Blue = 50 Then ToolText = "Open Editor Window" 'Editor Button has been released
        If Red = 0 And Green = 100 And Blue = 0 Then ToolText = "Move to Start of Animation" 'To Start Button has been released
        If Red = 100 And Green = 0 And Blue = 0 Then ToolText = "Move Back 1 Frame" 'Back 1 Button has been released
        If Red = 0 And Green = 50 And Blue = 0 Then ToolText = "Stop Playing Animation" 'Stop Button has been released
        If Red = 50 And Green = 0 And Blue = 0 Then ToolText = "Play Animation" 'Play Button has been released
        If Red = 0 And Green = 0 And Blue = 50 Then ToolText = "Move Forward 1 Frame" 'Forward 1 Button has been released
        If Red = 0 And Green = 0 And Blue = 100 Then ToolText = "Move To End of Animation" 'To end Button has been released
        If Red = 100 And Green = 100 And Blue = 0 Then ToolText = "Loop the Last Few Frames" 'Loop Button has been released
        If Red = 200 And Green = 0 And Blue = 255 Then ToolText = "Open RotoScope Form" 'Rotoscope Button has been released
        If Red = 46 And Green = 8 And Blue = 144 Then ToolText = "Undo Last Frame" 'Undo Button has been released
        If Red = 246 And Green = 147 And Blue = 134 Then ToolText = "Turn Sound On/Off" 'Sound Toggle Button has been released
        If Red = 79 And Green = 14 And Blue = 11 Then ToolText = "Turn X Sheet On/Off" 'X Sheet Toggle Button has been released
        If Red = 241 And Green = 250 And Blue = 0 Then ToolText = "Open Chroma Key Controls" 'Chroma Key button has been released

        If MousePoint.X > Me.picFrameSlider.Left And MousePoint.X < Me.picFrameSlider.Left + Me.picFrameSlider.Width Then
            If MousePoint.Y > Me.picFrameSlider.Top And MousePoint.Y < Me.picFrameSlider.Top + Me.picFrameSlider.Height Then
                ToolText = "Scroll Through Captured Frames" 'You are over the frame scroller
            End If
        End If

        If MousePoint.X > Me.picOnionSlider.Left And MousePoint.X < Me.picOnionSlider.Left + Me.picOnionSlider.Width Then
            If MousePoint.Y > Me.picOnionSlider.Top And MousePoint.Y < Me.picOnionSlider.Top + Me.picOnionSlider.Height Then
                ToolText = "Select Level of Onion Skinning" 'You are over the frame scroller
            End If
        End If

        If MousePoint.X > Me.picThreashold.Left And MousePoint.X < Me.picThreashold.Left + Me.picThreashold.Width Then
            If MousePoint.Y > Me.picThreashold.Top And MousePoint.Y < Me.picThreashold.Top + Me.picThreashold.Height Then
                ToolText = "Set Chroma Key Threashold" 'You are over the Threashold scroller
            End If
        End If

        If MousePoint.X > Me.picFeather.Left And MousePoint.X < Me.picFeather.Left + Me.picFeather.Width Then
            If MousePoint.Y > Me.picFeather.Top And MousePoint.Y < Me.picFeather.Top + Me.picFeather.Height Then
                ToolText = "Set Chroma Key Feather Edge" 'You are over the Threashold scroller
            End If
        End If

        If MousePoint.X > Me.picWhiteLimit.Left And MousePoint.X < Me.picWhiteLimit.Left + Me.picWhiteLimit.Width Then
            If MousePoint.Y > Me.picWhiteLimit.Top And MousePoint.Y < Me.picWhiteLimit.Top + Me.picWhiteLimit.Height Then
                ToolText = "Set Chroma Key White Limit" 'You are over the Threashold scroller
            End If
        End If

        If MousePoint.X > Me.picBlackLimit.Left And MousePoint.X < Me.picBlackLimit.Left + Me.picBlackLimit.Width Then
            If MousePoint.Y > Me.picBlackLimit.Top And MousePoint.Y < Me.picBlackLimit.Top + Me.picBlackLimit.Height Then
                ToolText = "Set Chroma Key Black Limit" 'You are over the Threashold scroller
            End If
        End If

        lblLCD1.Text = ProjName & ".avi" & Chr(13) & Chr(10) & FrameNum & " Frame(s) Captured" & Chr(13) & Chr(10) & ToolText

    End Sub
    Private Sub ButtonWindowDragging() 'Enables the user to drag the button window around the screen
        If GetAsyncKeyState(VK_LBUTTON) Then 'Check the Left mouse is being pressed
            If DragWindow(Me.picFacia.Handle.ToInt32, Hotspot_Buttons) = True Then 'If true, the mouse is over the body of the button window and not over a button.
                ReleaseCapture() 'Release the mouse button from the previous command
                SendMessage(Me.Handle.ToInt32, WM_NCLBUTTONDOWN, HTCAPTION, 0) 'Begin dragging the form until mouse button is released
            End If
        End If
    End Sub
    Private Sub PositionOnionSlider() 'Actions the Onion slider

        If GetAsyncKeyState(VK_LBUTTON) Then 'Check the Left mouse is being pressed

            Call GetCursorPos(MousePoint) 'Get the screen position of the cursor
            Call ScreenToClient(Me.picOnionSlider.Handle.ToInt32, MousePoint) 'Modify values so they are relative to top left of Buttons picture window
            If MousePoint.X > 0 And MousePoint.X < Me.picOnionSlider.Width Then 'check if we are over the slider picturebox
                If MousePoint.Y > 0 And MousePoint.Y < Me.picOnionSlider.Height Then

                    If ActiveWindowIs = "ButtonWindow" Then
                        If WindowAtFront = "Playback" Then Call Toggle() 'The playback window is showing, so to flip to the live window
                        'We know how far we are along the picturebox (mouse.x) and we know the width, so calculate the percentage along (value 0 to 1)

                        OnionLevel = Int((MousePoint.X / Me.picOnionSlider.Width) * 255) 'Values are modified, so that its easy to obtain 0 and 100% at the ends of the slider
                        If OnionLevel < 0 Then OnionLevel = 0 'make sure value is between 0 and 255 (for alphablend function)
                        If OnionLevel > 255 Then OnionLevel = 255
                        'UPGRADE_WARNING: Couldn't resolve default property of object OnionSliderPos. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
                        OnionSliderPos = MousePoint.X 'set the variable to the current x position
                        Call DrawOnionSlider() 'draw the slider (use the cursor position for more accuracy of slider to cursor)
                    End If
                End If
            End If

        End If

    End Sub
    Public Sub DrawOnionSlider() 'Position the onion slider indication mark

        'UPGRADE_WARNING: Couldn't resolve default property of object OnionSliderPos. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
        If OnionSliderPos < 3 Then OnionSliderPos = 3 'Limit the display of the slider so it doesnt go over the end
        'UPGRADE_WARNING: Couldn't resolve default property of object OnionSliderPos. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
        If OnionSliderPos > Me.picOnionSlider.Width - 3 Then OnionSliderPos = Me.picOnionSlider.Width - 3 'Limit the display of the slider so it doesnt go over the end

        Dim onionGraph As Graphics = frmAveraging.picOnion1.CreateGraphics
        Dim onionhdc As IntPtr = onionGraph.GetHdc
        Dim sliderGraph As Graphics = frmAveraging.picSlider.CreateGraphics
        Dim sliderhdc As IntPtr = sliderGraph.GetHdc

        'UPGRADE_ISSUE: Constant vbWhiteness was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="55B59875-9A95-4B71-9D6A-7C294BF7139D"'
        'UPGRADE_ISSUE: PictureBox property picOnionSlider.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        Call BitBlt(onionhdc, 0, 0, Me.picOnionSlider.Width, Me.picOnionSlider.Height, 0, 0, 0, vbWhiteness) 'overwite the slider to erase the previous position
        'UPGRADE_ISSUE: Constant vbSrcCopy was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="55B59875-9A95-4B71-9D6A-7C294BF7139D"'
        'UPGRADE_ISSUE: PictureBox property picSlider.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'UPGRADE_WARNING: Couldn't resolve default property of object OnionSliderPos. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
        'UPGRADE_ISSUE: PictureBox property picOnionSlider.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        Call BitBlt(onionhdc, OnionSliderPos - 3, 1, 6, 13, sliderhdc, 0, 0, vbSrcCopy) 'blit the slider to the screen
        Me.picOnionSlider.Refresh() 'Refresh the slider control to show the changes

        onionGraph.ReleaseHdc(onionhdc)
        onionGraph.Dispose()

        sliderGraph.ReleaseHdc(sliderhdc)
        sliderGraph.Dispose()

    End Sub
    Private Sub ActionFrameSlider()
        'Now action the frame slider
        If FrameNum < 1 Then Exit Sub 'No frames have been captured, so take no action

        If GetAsyncKeyState(VK_LBUTTON) Then 'Check the Left mouse is being pressed

            Call GetCursorPos(MousePoint) 'Get the screen position of the cursor
            Call ScreenToClient(Me.picFrameSlider.Handle.ToInt32, MousePoint) 'Modify values so they are relative to top left of Buttons picture window
            If MousePoint.X > 0 And MousePoint.X < Me.picFrameSlider.Width Then 'check if we are over the slider picturebox
                If MousePoint.Y > 0 And MousePoint.Y < Me.picFrameSlider.Height Then
                    Call AudioPlayer_Stop() 'Stop the soundtrack playing
                    If WindowAtFront = "Live" Then Call Toggle() 'The live window is showing, so to flip to the playback window
                    'We know how far we are along the picturebox (mouse.x) and we know the width, so calculate the percentage along (value 0 to 1)
                    'multiply this by the total .avi length to get the time in seconds where the position should be
                    TimePosition = ((MousePoint.X) / Me.picFrameSlider.Width) * PlayBackMediaPosition.Duration
                    If TimePosition < 0 Then TimePosition = 0 'check we havent calculated it out of the boundary limits
                    If TimePosition > PlayBackMediaPosition.Duration Then TimePosition = PlayBackMediaPosition.Duration

                    PlayBackMediaPosition.CurrentPosition = TimePosition 'set the avi file to the new position
                    PlayBackMediaControl.Pause() 'pause the .avi file
                    PlayStatus = "Stopped"
                    Call PositionFrameSlider(2) 'draw the slider (use the cursor position for more accuracy of slider to cursor)

                    CopyCamToPic1() 'Required otherwise screen doesn't update with new image as you move the slider.
                    'I dont really understand why. Probably it waits for camera to action
                    'before refreshing screens so trick it using this small call.
                End If
            End If
        End If
    End Sub
    Public Sub PositionFrameSlider(ByRef Mode As Short) 'Position Slider correctly
        ' This command can be called in two modes
        'Mode 1 - It calculates the slider position from the position we are in the avi file
        'Mode 2 - It calculates it from the position of the cursor over the slider

        If Mode = 1 Then 'Calculate the slider position relative to the current position in the .avi file
            FrameSliderPosition = CInt((CDbl(PlayBackMediaPosition.CurrentPosition) / CDbl(PlayBackMediaPosition.Duration)) * Me.picFrameSlider.Width)
        End If

        If Mode = 2 Then 'Calculate the slider position using the cursor position
            FrameSliderPosition = MousePoint.X
        End If

        If FrameSliderPosition < 3 Then FrameSliderPosition = 3 'Limit the display of the slider so it doesnt go over the end
        If FrameSliderPosition > Me.picFrameSlider.Width - 3 Then FrameSliderPosition = Me.picFrameSlider.Width - 3 'Limit the display of the slider so it doesnt go over the end

        Dim meGraph As Graphics = Me.picFrameSlider.CreateGraphics
        Dim mehdc As IntPtr = meGraph.GetHdc
        Dim avgGraph As Graphics = frmAveraging.picSlider.CreateGraphics
        Dim avghdc As IntPtr = avgGraph.GetHdc

        'UPGRADE_ISSUE: Constant vbWhiteness was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="55B59875-9A95-4B71-9D6A-7C294BF7139D"'
        'UPGRADE_ISSUE: PictureBox property picFrameSlider.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        Call BitBlt(mehdc, 0, 0, Me.picFrameSlider.Width, Me.picFrameSlider.Height, 0, 0, 0, vbWhiteness) 'overwite the slider to erase the previous position
        'UPGRADE_ISSUE: Constant vbSrcCopy was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="55B59875-9A95-4B71-9D6A-7C294BF7139D"'
        'UPGRADE_ISSUE: PictureBox property picSlider.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        'UPGRADE_ISSUE: PictureBox property picFrameSlider.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        Call BitBlt(mehdc, FrameSliderPosition - 3, 1, 6, 13, avghdc, 0, 0, vbSrcCopy) 'blit the slider to the screen
        Me.picFrameSlider.Refresh() 'Refresh the slider control to show the changes

        meGraph.ReleaseHdc(mehdc)
        meGraph.Dispose()
        avgGraph.ReleaseHdc(avghdc)
        avgGraph.Dispose()

    End Sub

    Private Sub ActionRotoscoping() 'Perform all the rotoscoping functions
        If RotoScopeLoadedYesNO = False Then Exit Sub 'Check if rotoscope has been loaded with a .avi. If not take no actions and exit sub
        If RotoScopeSyncYesNo = True Then
            frmRotoscope.Text = "Rotoscope - Sync On  " & ((Int(RotoScopeMediaPosition.CurrentPosition * 1000)) / 1000) & " Seconds"
        Else
            frmRotoscope.Text = "Rotoscope - Sync Off " & ((Int(RotoScopeMediaPosition.CurrentPosition * 1000)) / 1000) & " Seconds"
        End If


        'Check if the rotoscope window has been resized. if so, adjust the image in the window to the correct size
        If (VB6.PixelsToTwipsX(frmRotoscope.Width) / VB6.TwipsPerPixelX) <> dx_VideoWindow.Width Then
            dx_VideoWindow.Width = (VB6.PixelsToTwipsX(frmRotoscope.Width) / VB6.TwipsPerPixelX)
            dx_VideoWindow.Height = dx_VideoWindow.Width / AspRatio
        End If

    End Sub
    Private Sub PlayBackModeLooping() 'Handle the playback looping function
        If FrameNum < 1 Then Exit Sub 'If there are no frames captured so take no action
        If WindowAtFront = "Live" Then Exit Sub 'The live window is showing, so there is no playback happening.
        If (PlayStatus = "Looping") And (PlayBackMediaPosition.CurrentPosition = PlayBackMediaPosition.Duration) Then 'The playback is looping and has reached the end of the avi file
            ProgCurrentTime = GetTickCount() 'Get the current time
            If ShowingLive = False Then
                'first time program reaches end of avi file playback window will be showing so flip to the live window and set up the LiveTime counter
                LiveTime = ProgCurrentTime + 1000 'Set the livetime to the current time + 1 second
                frmCapture.picLive.Visible = True 'show the live window
                frmCapture.picPlayBack.Visible = False 'hide the playback window
                ShowingLive = True 'Set flag that live is now showing
            Else
                'The windows have been flipped to show the live window, so check to see if it is time to loop back again.
                If ProgCurrentTime > LiveTime Then 'Check the time has elapsed, ie that the current time has gone passed what was set in the LiveTime variable
                    frmCapture.picPlayBack.Visible = True 'Show the playback window
                    frmCapture.picLive.Visible = False 'Hide the Live window
                    ShowingLive = False 'Set the flag that the playback window is now showing again
                    Call LoopButt() 'begin looping again
                End If
            End If
        End If
        'if you pause the looping function whilst showing live, we need to flip the live window back to the playback window.
        If ShowingLive = True And PlayStatus <> "Looping" Then
            frmCapture.picPlayBack.Visible = True
            frmCapture.picLive.Visible = False
        End If

    End Sub

    Private Sub tmrTimeLapse_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles tmrTimeLapse.Tick
        'This is the time lapse routine
        If IsCapturing = True Then Exit Sub 'Program is still capturing, so dont trigger / buffer up another capture
        Capture = False 'Call action
    End Sub

    Private Sub ActionExposureSheet() 'This update the exposure sheet every cycle so it scrolls and shows the correct mouth shape
        'Handles drawing at any time whether in editor or main window
        Dim CurrentlyShowingFrame As Integer
        ' Dim PhonemePoint As String

        If XSheetActiveYesNo = False Then Exit Sub 'If the exposuresheet is hidden take no action

        If ActiveWindowIs = "EditorWindow" And RapidStatus <> "Playing" Then
            'We are in the editor and not in rapid play mode
            If EditorListPosition + XSheetOffset <= UBound(Phoneme) And EditorListPosition + XSheetOffset >= 0 Then
                Call frmXSheet.LoadMouthShape(Phoneme(EditorListPosition + XSheetOffset)) 'Draw the mouthshape, send it the full text from the correct line in the phoneme sheet
            Else
                Call frmXSheet.LoadMouthShape("   ") 'Draw the mouthshape, just send blank text so he rest position mouth is drawn
            End If
            If EditorListPosition + XSheetOffset <> LastXSheetPos Then 'Only print if the sheet has changed position, this avoids flickering
                Call frmXSheet.ReloadExposureSheet(EditorListPosition + XSheetOffset)
                LastXSheetPos = EditorListPosition + XSheetOffset
            End If
            Exit Sub
        End If

        If ActiveWindowIs = "EditorWindow" And RapidStatus = "Playing" Then
            'We are in the editor in fast playback mode
            'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
            If EditorMediaControl Is Nothing Then
                Call frmXSheet.LoadMouthShape("   ") 'Justs show the rest position mouth
                Exit Sub 'If the media player doesn't exist exit subroutine
            End If
            CurrentlyShowingFrame = Fix((EditorMediaPosition.CurrentPosition * FPS) + 0.00001) + XSheetOffset 'calculate the currently showing frame and add the xsheet offset
            If CurrentlyShowingFrame < 0 Or CurrentlyShowingFrame > UBound(Phoneme) Then
                Call frmXSheet.LoadMouthShape("   ") 'Justs show the rest position mouth
            Else
                Call frmXSheet.LoadMouthShape(Phoneme(CurrentlyShowingFrame))
            End If

            Result = (Fix((EditorMediaPosition.CurrentPosition * FPS) + 0.00001))
            If Result < 0 Then Result = 0
            If Result > UBound(Phoneme) Then Result = UBound(Phoneme)
            If Result <> LastXSheetPos Then 'Only print if the sheet has changed position, this avoids flickering
                Call frmXSheet.ReloadExposureSheet(Result)
                LastXSheetPos = Result
            End If

            Exit Sub
        End If


        If ActiveWindowIs = "ButtonWindow" And WindowAtFront = "Live" Then
            'The live window is showing
            Call frmXSheet.LoadMouthShape(VB6.GetItemString(frmXSheet.lstExposure, 1)) 'Draw the mouthshape, send it the full text from the second line down in x sheet
            Call frmXSheet.ReloadExposureSheet(FrameNum)
            Exit Sub
        End If


        If ActiveWindowIs = "ButtonWindow" And WindowAtFront = "PlayBack" Then
            'The playback window is showing. We always use the avi video in this mode to show image so calcualte mouthshape from video position.
            'UPGRADE_ISSUE: ObjPtr function is not supported. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="367764E5-F3F8-4E43-AC3E-7FE0B5E074E2"'
            If PlayBackMediaControl Is Nothing Then
                Call frmXSheet.LoadMouthShape("   ") 'Justs show the rest position mouth
                Exit Sub 'If the media player doesn't exist exit subroutine
            End If

            CurrentlyShowingFrame = Fix((PlayBackMediaPosition.CurrentPosition * FPS) + 0.00001) + XSheetOffset 'calculate the currently showing frame and add the xsheet offset
            If CurrentlyShowingFrame < 0 Or CurrentlyShowingFrame > UBound(Phoneme) Then
                Call frmXSheet.LoadMouthShape("   ") 'Justs show the rest position mouth
            Else
                Call frmXSheet.LoadMouthShape(Phoneme(CurrentlyShowingFrame))
            End If

            Result = (Fix((PlayBackMediaPosition.CurrentPosition * FPS) + 0.00001))
            If Result < 0 Then Result = 0
            If Result > UBound(Phoneme) Then Result = UBound(Phoneme)
            If Result <> LastXSheetPos Then 'Only print if the sheet has changed position, this avoids flickering
                Call frmXSheet.ReloadExposureSheet(Result)
                LastXSheetPos = Result
            End If
            Exit Sub
        End If

    End Sub

    Private Sub DrawSafeRectangle(ByRef TargetPicture1 As System.Windows.Forms.PictureBox, ByRef RectWidth As Integer, ByRef RectHeight As Integer, ByRef RectColour As Integer) 'Draw a safe area rectangle.

        If SafeAreaYesNo = False Then Exit Sub 'If the user doesn't want a rectangle exit the subroutine

        Dim p As POINT ' Create the  point variable for MoveToEx API
        Dim LeftPos As Integer
        Dim RightPos As Integer
        Dim TopPos As Integer
        Dim BottomPos As Integer

        If (RectWidth > CaptureWidth) Or (RectWidth < 1) Then RectWidth = CaptureWidth
        If (RectHeight > CaptureHeight) Or (RectHeight < 1) Then RectHeight = CaptureHeight

        LeftPos = (CaptureWidth - RectWidth) / 2
        RightPos = LeftPos + RectWidth
        TopPos = (CaptureHeight - RectHeight) / 2
        BottomPos = TopPos + RectHeight

        frmAveraging.picBufferMain.ForeColor = System.Drawing.ColorTranslator.FromOle(RectColour) '&H4080FF                   'Set the pen colour

        Dim targetGraph As Graphics = TargetPicture1.CreateGraphics
        Dim targethdc As IntPtr = targetGraph.GetHdc

        'UPGRADE_WARNING: Couldn't resolve default property of object p. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
        'UPGRADE_ISSUE: PictureBox property TargetPicture1.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        Call MoveToEx(targethdc, LeftPos, TopPos, p)
        'UPGRADE_ISSUE: PictureBox property TargetPicture1.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        Call LineTo(targethdc, RightPos, TopPos)
        'UPGRADE_ISSUE: PictureBox property TargetPicture1.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        Call LineTo(targethdc, RightPos, BottomPos)
        'UPGRADE_ISSUE: PictureBox property TargetPicture1.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        Call LineTo(targethdc, LeftPos, BottomPos)
        'UPGRADE_ISSUE: PictureBox property TargetPicture1.hdc was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        Call LineTo(targethdc, LeftPos, TopPos)

        targetGraph.ReleaseHdc(targethdc)
        targetGraph.Dispose()

    End Sub
End Class